/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite;

import java.util.Map;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.Type;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.api.Controller;

/**
 * Provides a basic implementation of the {@link Component} interface.
 */
public class BasicComponent implements Controller, Component {

  /**
   * The type of this component.
   */
  public Type fcType;

  /**
   * The interfaces of this component. This map associates each interface to its
   * name.
   */
  public Map<String, Interface> fcInterfaces;

  /**
   * @param ic information about the component to which this controller object
   *      belongs.
   * @throws InstantiationException if the initialization fails.
   */
  public void initFcController (final InitializationContext ic) throws InstantiationException {
    this.fcType = ic.type;
    this.fcInterfaces = ic.interfaces;
  }

  public Type getFcType () {
    return fcType;
  }

  public Object[] getFcInterfaces () {
    return fcInterfaces.values().toArray();
  }

  public Object getFcInterface (final String interfaceName) throws NoSuchInterfaceException {
    Object itf = fcInterfaces.get(interfaceName);
    if (itf == null) {
      throw new ChainedNoSuchInterfaceException(null, this, interfaceName);
    }
    return itf;
  }

}
